<template>
	<view class="userinfo" :style="{ paddingTop: paddingHeight + 'px' }">
		<nav-bar></nav-bar>
		<view class="title">请确认您的信息！</view>
		<view class="card">
			<view class="card-item">
				<view class="l">姓名</view>
				<view class="r">{{ userInfo.name }}</view>
			</view>
			<view class="card-item">
				<view class="l">出生日期</view>
				<view class="r">{{ userInfo.birthday }}</view>
			</view>
			<view class="card-item">
				<view class="l">性别</view>
				<view class="r">{{ userInfo.sex == 1 ? '男' : '女' }}</view>
			</view>
            <view class="card-item">
				<view class="l">身高</view>
				<view class="r">{{ userInfo.height }}</view>
			</view>
			<view class="card-item">
				<view class="l">体重</view>
				<view class="r">{{ userInfo.weight }}</view>
			</view>
			<view class="card-item">
				<view class="l">籍贯</view>
				<view class="r">{{ userInfo.birthPlace }}</view>
			</view>
		</view>
		<view class="card">
			<view class="card-item">
				<view class="l">睡觉时间</view>
				<view class="r">{{ sleep }}</view>
			</view>
			<view class="card-item">
				<view class="l">起床时间</view>
				<view class="r">{{ wake }}</view>
			</view>
			<view class="card-item">
				<view class="l">静息心率（次/min）</view>
				<view class="r">{{userInfo.heartRate }}</view>
			</view>
            <view class="card-item">
				<view class="l">你习惯的菜系</view>
				<view class="r">{{ cuisineName }}</view>
			</view>
		</view>
		<view class="card">
			<view class="card-item">
				<view class="l">您使用的目的</view>
				<view class="r">{{ goalCheckObj[userInfo.goal]  }}</view>
			</view>
			<view class="card-item">
				<view class="l">您平时的运动强度</view>
				<view class="r">{{ palCheckObj[userInfo.pal] }}</view>
			</view>
		</view>
        <view class="btn-box">
            <button open-type="getPhoneNumber" @getphonenumber=" getPhoneNumber " plain="true" class="next-btn2"
                @click=" getPhoneNumber ">完成</button>
            <view class="return-btn" @click=" goBack ">返回</view>
        </view>
	</view>
</template>

<script>
import { getCuisineMsg, registerUser, getWxPhone } from '@/api/index.js'
export default {
	data() {
        return {
            paddingHeight: uni.getStorageSync('navbarHeight'),
            imgUrl: this.$imgUrl,
            showDate: false,
            userId: uni.getStorageSync("userId"),
            userInfo: {
                avatar: '',
                name: '',
                birthday: '',
                sleepHour: null,
                sleepMin: null,
                wakeHour: null,
                wakeMin: null,
                weight: null,
                height: null,
                sex: 1,
                goal: null, //目的
                birthPlace: null, //籍贯
                cuisineId: null, //菜系id
                heartRate: null, //心率
                pal: null, //运动强度
                phone: null, //手机号
            },
            cuisineName: null, //菜系
            sleep: '00:00',
            wake: '00:00',
            palCheckObj: {
                '1':'极低',
                '2':'低',
                '3':'中',
                '4':'高',
            },
            goalCheckObj: {
                '1':'提高代谢力',
                '2':'提高自愈力',
                '3':'减肥',
                '4':'增重',
            }
        }
	},
	mounted() {
		this.getCuisineMsgData()
	},
	onLoad(OPTIONS) {
		if (OPTIONS) {
			this.userInfo = Object.assign(
				{},
				this.userInfo,
				JSON.parse(OPTIONS.userInfo)
			)
			this.sleep = this.userInfo.sleepHour + ':' + this.userInfo.sleepMin
			this.wake = this.userInfo.wakeHour + ':' + this.userInfo.wakeMin
		}
	},
	methods: {
		getPhoneNumber(e) {
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				let params = {
					code: e.detail.code,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv
				}
				getWxPhone(params).then((res) => {
					this.userInfo.phone = res.data
					this.next()
				})
			}
			if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
				this.next()
			}
		},
		async next() {
			let params = {
				...this.userInfo,
				userId: this.userId
			}
			let str = this.userInfo.birthday
				.replace(/年|月/g, '-')
				.replace(/日/g, '')
			let arr = str.split('-')
			let newarr = arr.map((item, index) => {
				if (index != 0) {
					if (item.length <= 1) {
						return '0' + item
					} else {
						return item
					}
				} else {
					return item
				}
			})
			params.birthday = newarr.join('-')
			try {
				const res = await registerUser(params)
				console.log(res)
				if (res.code == 200) {
					uni.reLaunch({
						url: '/pagesB/login/device/search/index?type=login'
					})
				}
			} catch (e) {
				//TODO handle the exception
			}
		},
		// 获取菜系
		getCuisineMsgData() {
			getCuisineMsg().then((res) => {
				res.data.forEach((val) => {
					if (this.userInfo.cuisineId == val.id) {
						this.cuisineName = val.cuisineName
					}
				})
			})
		},
		goBack() {
			uni.navigateBack({
				delta: 1 // 默认值是1，表示回到上一页
			})
		}
	}
}
</script>

<style lang="scss">
page {
	background-color: #f7f7f7;
}
</style>
<style lang="scss" scoped>
.userinfo {
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	padding: 0 48rpx 144rpx;
	background: #f3f4f7;
	.title {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 38rpx;
		color: #000000;
		text-align: left;
		width: 100%;
		padding: 10rpx 38rpx;
	}
	.card {
        width: 100%;
		padding: 0 38rpx;
        background: #FFFFFF;
        border-radius: 15rpx 15rpx 15rpx 15rpx;
        margin-top: 20rpx;
		.card-item {
			padding: 24rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
            border-bottom: 2rpx solid #F0F1F5;
            &:last-child{
                border-bottom: 0;
            }
			.l {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #1f2434;
			}
			.r {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: rgba(31, 36, 52, 0.5);
			}
		}
	}
    .btn-box {
        width: 100%;
        padding: 64rpx 32rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .return-btn {
            width: 242rpx;
            height: 76rpx;
            border-radius: 38rpx;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 28rpx;
            color: #1F2434;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .next-btn2 {
            width: 424rpx;
            height: 85rpx;
            background: #1F2434;
            border-radius: 38rpx;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 28rpx;
            color: #FFFFFF;
            display: flex;
            justify-content: center;
            align-items: center;
            border: none;
        }
    }
}
</style>
